package de.hsowl.tod4a.global.xml;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import de.hsowl.tod4a.global.model.ServerProperty;

public abstract class DefaultHandlerHelper extends DefaultHandler {

	private DataHelper data;

	public DefaultHandlerHelper(ServerProperty prop) {
		data = new DataHelper(prop);
	}

	public void startDocument() throws SAXException {
		super.startDocument();
	}

	public void startElement(String uri, String localName, String name, Attributes atts) throws SAXException {
		super.startElement(uri, localName, name, atts);
		startElement(localName, name, new AttributeHelper(atts, data));
	}

	public void endElement(String uri, String localName, String name) throws SAXException {
		super.endElement(uri, localName, name);
		localName = data.restoreString(localName);
		name = data.restoreString(name);
		endElement(localName, name);
	}

	public void characters(char[] ch, int start, int length) throws SAXException {
		super.characters(ch, start, length);
		String text = new String(ch, start, length);
		
		if(text.startsWith("__")){
			System.out.println();
		}
		
		if (text.equals("\n") || text.trim().equals("")) {
			return;
		}
		text = data.restoreString(text);
		characters(text);
	}

	public abstract void startElement(String localName, String name, AttributeHelper atts);

	public abstract void characters(String txt);

	public abstract void endElement(String localName, String name);
}
